package com.mfm.controller;

import com.mfm.pojo.Book;
import com.mfm.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: MFM
 * @Date: 2022/8/1 - 08 - 01 - 17:43
 * @version: 1.0
 */

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    @PostMapping
    public Result save(@RequestBody Book book) {
        boolean flag = bookService.save(book);
        return new Result((flag ? code.SAVE_OK : code.SAVE_ERR), flag);
    }

    @PutMapping
    public Result update(@RequestBody Book book) {
        boolean flag = bookService.update(book);
        return new Result((flag ? code.UPDATE_OK : code.UPDATE_ERR), flag);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = bookService.delete(id);
        return new Result((flag ? code.DELETE_OK : code.DELETE_ERR), flag);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Book book = bookService.getById(id);
        return new Result((book != null ? code.GET_OK : code.GET_ERR), book, (book != null ? "" : "未查到结果"));
    }

    @GetMapping
    public Result getAll() {
        List<Book> books = bookService.getAll();
        return new Result((books != null ? code.GET_OK : code.GET_ERR), books, (books != null ? "" : "未查到结果"));
    }
}
